import Vue from 'vue'
import Vuex from 'vuex'
import {getTimeInterval} from '../utils/index'

Vue.use(Vuex)
// 略:后台获取系统运行时间
const runAt = '1600999211461';
let timer = null;
const state = {
    loading: false,
    fixed: true,  //用于控制页面header的背景色
    showLoginBox: false, //用于控制登录框的展示
    runTimeInterval: '',
    webConfigData: {}, // WebConfig网站配置
    userInfo: {},  // 用户信息
    isLogin: false
}
const mutations = {
    SET_LOADING: (state, v) => {
        state.loading = v;
    },
    SET_FIXED: (state, v) => {
        state.fixed = v
    },
    SET_SHOW_LOGIN_BOX: (state, v) => {
        state.showLoginBox = v
    },
    GET_RUNTIME_INTERVAL: (state) => {
        if (!timer || !state.runTimeInterval) {
            clearInterval(timer)
            timer = setInterval(() => {
                state.runTimeInterval = getTimeInterval(runAt);
            }, 1000);
        }
    },
    // 设置WebConfig
    SET_WEB_CONFIG_DATA:(state, v) =>{
        state.webConfigData = v
    },
    SET_USER_INFO:(state, v)=> {
        state.userInfo = v
    },
    //登录状态
    SET_LOGIN_STATUS:(state, v)=> {
        state.isLogin = v
    },
}
const actions = {
    setLoading: ({ commit }, v) => {
        commit('SET_LOADING', v);
    },
    setFixed: ({ commit }, v) => {
        commit('SET_FIXED', v);
    },
    initComputeTime: ({ commit }) => {
        commit('GET_RUNTIME_INTERVAL');
    },
    setWebConfigData: ({ commit }, v) => {
        commit('SET_WEB_CONFIG_DATA', v);
    }
}
const getters = {
    loading: state => state.loading,
    fixed: state => state.fixed,
    showLoginBox: state => state.showLoginBox,
    runTimeInterval: state => state.runTimeInterval,
    webConfigData: state => state.webConfigData,
    userInfo: state => state.userInfo,
    isLogin: state => state.isLogin,
}
export default new Vuex.Store({
    state,
    mutations,
    actions,
    modules: {},
    getters
})
